package com.example.day16;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.squareup.picasso.Picasso;

import java.util.List;
import java.util.Map;

public class MyAdapter extends BaseAdapter {
    Context context;
    List<Map<String,Object>> list1;
    DisplayImageOptions options;
     // 创建ImageLoader对象
    private ImageLoader imageLoader = ImageLoader.getInstance();
    public MyAdapter(Context context, List<Map<String,Object>> list1){
        this.context=context;
        this.list1=list1;

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)// 设置图片下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher_round)// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.ic_launcher_round)// 设置图片加载或解码过程中发生错误显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .displayer(new RoundedBitmapDisplayer(80))// 设置成圆角图片
                .build();// 创建DisplayImageOptions对象
    }
    @Override
    public int getCount() {
        return list1.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder ;


        if (convertView == null) {
            convertView = View.inflate(context, R.layout.test_item, null);
            viewHolder = new ViewHolder();
            viewHolder.textView = convertView.findViewById(R.id.text1);
            viewHolder.textView1 = convertView.findViewById(R.id.text2);
            viewHolder.image = convertView.findViewById(R.id.image1);
            convertView.setId(position);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();//取出ViewHolder对象
        }
        Log.e("###MyListViewBase", "getView " + position + " " + convertView.getId());
        Map<String,Object> map= list1.get(position);
        if(viewHolder!=null){
            viewHolder.textView.setText(map.get("title").toString());
            viewHolder.textView1.setText(map.get("food_str").toString());
//            viewHolder.image.setImageBitmap((Bitmap)map.get("pic"));
            Picasso.with(context)
                    .load(map.get("pic").toString())
                    .into(viewHolder.image);
            // 使用ImageLoader加载图片
            imageLoader.displayImage(map.get("pic").toString(),viewHolder.image, options);
        }

        return convertView;
    }

    public class ViewHolder{
        TextView textView;
        TextView textView1;
        ImageView image;
    }
}
